package algorithm.tree;

import org.junit.Test;

/**
 * 二叉树翻转
 *
 * @Author: zhenzhong.wang
 * @Date: 10/8/2018 3:28 PM
 */
public class InvertBinaryTree {

  public TreeNode invert(TreeNode node) {
    if (node == null) {
      return null;
    }
    node.setLeft(invert(node.getLeft()));
    node.setRight(invert(node.getRight()));

    TreeNode midNode = node.getLeft();
    node.setLeft(node.getRight());
    node.setRight(midNode);

    return node;
  }

  @Test
  public void invertTest() {
    // 假设treeNode为已初始化的二叉树根节点
    TreeNode treeNode = new TreeNode(1);
    // 翻转二叉树
    TreeNode invertedNode = invert(treeNode);
  }
}
